package com.bocai.dahuatech.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.ProductSeriesListBean_new;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.ui.adapter.ProductSecendAdapter;
import com.bocai.dahuatech.utils.AESUtils;
import com.google.gson.Gson;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ProductSecendActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    @Bind(R.id.lv_product_secend)
    ListView mListView;
    private String title;
    private int parentId;
    ProductSeriesListBean_new bean=new ProductSeriesListBean_new();
    @Override
    protected int contentView() {
        return R.layout.activity_product_secend;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        title=getIntent().getStringExtra("text");
        parentId=getIntent().getIntExtra("parentId",-1);
        getDatas();
        initToolbar(title);
        initEvent();
    }
    @Override
    public void onEventMainThread(MainEvent event)
    {
        super.onEventMainThread(event);
        if(event.msg.equals("goshopcart"))
        {
            finish();
        }
    }
    private void getDatas() {
        getTestApi().getProductSeriesList(2,parentId)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error",e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean) {
            if (baseBean.getReturnNo().equals("1"))
            {
                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result",result);

                bean=new Gson().fromJson(result,ProductSeriesListBean_new.class);
                mListView.setAdapter(new ProductSecendAdapter(ProductSecendActivity.this,bean.getContent()));
            }else
            {
                showToast(baseBean.getReturnInfo());
            }
        }
    };

    @Override
    protected void initEvent() {
        super.initEvent();
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent=new Intent(this,ProductThirdActivity.class);
        intent.putExtra("text",bean.getContent().get(position).getFItemName());
        intent.putExtra("fid",bean.getContent().get(position).getFID());
        intent.putExtra("key","");
        intent.putExtra("screetype",bean.getContent().get(position).getFScreenType());
        startActivity(intent);
    }
}
